package com.helper.dream.a361phonehelper.ui;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.helper.dream.a361phonehelper.R;
import com.helper.dream.a361phonehelper.fragment.GuanliFragment;
import com.helper.dream.a361phonehelper.fragment.RedianFragment;
import com.helper.dream.a361phonehelper.fragment.RuanjianFragment;
import com.helper.dream.a361phonehelper.fragment.TuijianFragment;
import com.helper.dream.a361phonehelper.fragment.YouxiFragment;

/**
 * Created by LamYu on 16/12/19/0019.
 */

public class MainActivity extends AppCompatActivity {
    //底部导航文字数组
    private String[] tabTexts={
            "推荐","游戏","软件","热点","管理"
    };
    //底部导航的图片id数组
    private int[] imgIDs={
        R.drawable.tab_tuijian_sel,R.drawable.tab_youxi_sel,R.drawable.tab_ruanjian_sel,R.drawable.tab_redian_tab,R.drawable.tab_guanli_sel
    };
    //tabhost对应的fragment
    Class[] fragments={
            TuijianFragment.class, YouxiFragment.class, RuanjianFragment.class, RedianFragment.class, GuanliFragment.class
    };
    FragmentTabHost tabHost;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        setupViews();
    }
    LayoutInflater inflater;
    private void setupViews() {
        if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT){
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        tabHost= (FragmentTabHost) findViewById(android.R.id.tabhost);
        //设置tabhost
        tabHost.setup(this,getSupportFragmentManager(),R.id.fl_main);
        inflater=LayoutInflater.from(this);
        //给tabhost添加tab
        for (int i = 0; i < tabTexts.length; i++) {
            //创建tab
            TabHost.TabSpec tabItem=tabHost.newTabSpec(i+"");
            //给它设置内容
            tabItem.setIndicator(getTabItemView(i));
            tabHost.addTab(tabItem,fragments[i],null);
            //去边线
            tabHost.getTabWidget().setDividerDrawable(android.R.color.transparent);
        }
    }

    private View getTabItemView(int index) {
        View view=inflater.inflate(R.layout.tab_item_layout,null);
        //拿到控件中的值
        ImageView iv= (ImageView) view.findViewById(R.id.tab_img);
        iv.setImageResource(imgIDs[index]);
        TextView tv= (TextView) view.findViewById(R.id.tab_tv);
        tv.setText(tabTexts[index]);
        return view;
    }
}
